package com.yz.android_baozou.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yz.android_baozou.R;
import com.yz.android_baozou.activity.MainPageDetailActivity;
import com.yz.android_baozou.bean.UserSubmissionBean;
import com.yz.android_baozou.helper.RecyclerViewAdapterHelper;

import java.util.List;

/**
 * Created by Administrator on 2016/11/1.
 */

public class Home_User_RecyclerAdapter extends RecyclerViewAdapterHelper<UserSubmissionBean.DataBean> {
    private List<UserSubmissionBean.DataBean> mList_UserBean;
    private RecyclerView recyclerViewUserHome;

    public Home_User_RecyclerAdapter(Context context, List<UserSubmissionBean.DataBean> list, RecyclerView _recyclerViewUserHome) {
        super(context, list);
        this.mList_UserBean = list;
        this.recyclerViewUserHome=_recyclerViewUserHome;
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item2_homepage_home, parent, false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position=recyclerViewUserHome.getChildAdapterPosition(v);
                //根据点击的item的id拼接网址
                String url="http://baozouribao.com/documents/"+list.get(position).getDocument_id();
                Bundle bundle=new Bundle();
                bundle.putString("url",url);
                Intent intent=new Intent(context, MainPageDetailActivity.class);
                intent.putExtra("url",bundle);
                context.startActivity(intent);
            }
        });
        return new ViewHolder2(view);
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, int position) {
        String content = mList_UserBean.get(position).getTitle();
        String name = mList_UserBean.get(position).getAuthor_name();
        String show = mList_UserBean.get(position).getAuthor_avatar();
        if (holder instanceof ViewHolder2) {
            ((ViewHolder2) holder).textView_Content_item2.setText(content);
            ((ViewHolder2) holder).textView_author_item2.setText(name);
            Glide.with(context).load(show).into(((ViewHolder2) holder).imageView_show_item2);
        }
    }

    public UserSubmissionBean parseJsonToBean(String jsonString) {
        Gson gson = new Gson();
        return gson.fromJson(jsonString, new TypeToken<UserSubmissionBean>() {
        }.getType());
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {
        private ImageView imageView_show_item2;
        private TextView textView_Content_item2;
        private TextView textView_author_item2;

        public ViewHolder2(View itemView) {
            super(itemView);
            imageView_show_item2 = (ImageView) itemView.findViewById(R.id.imageView_show_item2);
            textView_Content_item2 = (TextView) itemView.findViewById(R.id.textView_Content_item2);
            textView_author_item2 = (TextView) itemView.findViewById(R.id.textView_author_item2);
        }
    }

}
